using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Musicman.Common.Entities;
using Musicman.Business;

namespace Musicman.Client2
{
    public partial class LoginForm : Form
    {
        private User appUser;

        public LoginForm()
        {
            InitializeComponent();
            User u = null;
            if (File.Exists(Program.UserPath) && (u = DeserializeUser()) != null)
            {
                this.txtEmail.Text = u.Emailaddress;
                this.txtPassword.Text = u.Password;
                this.cbRemeberMe.Checked = true;
                Program.RememberMe = true;
            }
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            User user = UserManager.Instance.LoggInUser(this.txtEmail.Text, this.txtPassword.Text, LoginType.Client);
            if (user != null)
            {
                this.appUser = user;
                this.DialogResult = DialogResult.OK;
                if (Program.RememberMe)
                {
                    using (FileStream fs = File.Open(Program.UserPath, FileMode.Create, FileAccess.Write))
                    {
                        BinaryFormatter bf = new BinaryFormatter();
                        bf.Serialize(fs, user);
                        fs.Flush();
                        fs.Close();
                    }
                }
                else if (!Program.RememberMe && File.Exists(Program.UserPath))
                {
                    File.Delete(Program.UserPath);
                }
            }
            else
            {
                this.lblError.Visible = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        public User DeserializeUser()
        {
            User u = null;
            try
            {
                using (FileStream fs = File.Open(Program.UserPath, FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    u = (User)bf.Deserialize(fs);
                    fs.Close();
                }
            }
            catch
            {
                File.Delete(Program.UserPath);
            }
            return u;
        }

        private void cbRemeberMe_CheckedChanged(object sender, EventArgs e)
        {
            Program.RememberMe = ((CheckBox)sender).Checked;
        }
        
        private void LoginForm_Paint(object sender, PaintEventArgs e)
        {
            Graphics gr = e.Graphics;
            SolidBrush sb = new SolidBrush(Color.Black);
            Pen p = new Pen(sb);
            p.Width = 2;
            gr.DrawRectangle(p, 1, 1, this.Width - 2 , this.Height - 2);
        }

        public User AppUser
        {
            get { return appUser; }
            set { appUser = value; }
        }
    }
}